import cv2
import matplotlib.pyplot as plt
import numpy as np
from holoviews.core.traversal import hierarchical
from numba import uint8
from sympy.abc import epsilon

def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

img = cv2.imread('C:/Users/nic/Desktop/opencv/picture/girl.jpg')
templet = cv2.imread('C:/Users/nic/Desktop/opencv/picture/templet_girl.jpg')
#获取templet的width和high
w,h = templet.shape[0:2]
#methods are cv2.TM_CCOEFF,cv2.TM_CCOEFF_NORMED,cv2.TM_CCORR,cv2.TM_CCORR_NORMED,cv2.TM_SQDIFF and cv2.TM_SQDIFF_NORMED
#模板匹配是去看模板在这张图片中和哪部分最相似
res = cv2.matchTemplate(img,templet,cv2.TM_CCORR_NORMED)
# 获取匹配结果中的最小值、最大值、最小值坐标和最大值坐标
min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(res)
#把最小值的坐标当成矩形框的左上角坐标
left_top = max_loc
#计算右下角坐标
right_bottom = (left_top[0]+w,left_top[1]+h)
#画出矩形框
img_1 = img.copy()
cv2.rectangle(img_1,left_top,right_bottom,(255,0,0),3)
#展示
cv_show('res',res)
cv_show('img',img)







